package com.ternnetwork.core.configuration.controller;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.configuration.dto.DataDictionaryAddDto;
import com.ternnetwork.core.configuration.dto.DataDictionaryEditDto;
import com.ternnetwork.core.configuration.service.DataDictionaryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/configuration/dataDictionary/*")
public class DataDictionaryController {

    @Autowired
    private DataDictionaryService dataDictionaryService;


    @PreAuthorize("hasAnyAuthority('API_DATA_DICTIONARY_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        DataDictionaryAddDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, DataDictionaryAddDto.class);
        return dataDictionaryService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_DATA_DICTIONAR_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        DataDictionaryEditDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, DataDictionaryEditDto.class);
        return dataDictionaryService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_DATA_DICTIONAR_FIND_ALL')")
    @RequestMapping("/findAll")
    public @ResponseBody BaseResponse findAll(Principal user)  {
      return dataDictionaryService.findAll();
    }

    @PreAuthorize("hasAnyAuthority('API_DATA_DICTIONAR_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id,String key) {
        return dataDictionaryService.deleteById(id,key);
    }



}
